<?php
/**
 * Pertyler
 * Object Browser's Table's Objects Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require '../library/Pert/General/Link.php';

require '../library/Pert/General/Page.php';

require '../databaseModel/Table.php';

require '../databaseModel/Role.php';

require '../control/main.php';

if(!\Pert\Page::Get()->HREF()->ParentTable)
	return false;

if(!\Pert\Page::Get()->HREF()->ParentRow)
	return false;

if(!\Pert\Page::Get()->HREF()->Table)
	return false;

if(!($moderator = Role_Moderator::Select()))
	if(require '../control/Login.php')
		return;

$parentTable = Table::Select(\Pert\Page::Get()->HREF()->ParentTable);

$parentRow = $parentTable->SelectRow(\Pert\Page::Get()->HREF()->ParentRow);

$table = $parentRow->SelectTable(\Pert\Page::Get()->HREF()->Table);

require 'KoolControls/KoolAjax/koolajax.php';

require 'KoolControls/KoolTreeView/kooltreeview.php';

$treeView = new \KoolTreeView('treeView');

$treeView->imageFolder = $iconsFolder;

$treeView->showLines = true;

if($table->ConnectorTable())
{
	$link = new \Pert\Link('ConnectorRow.php', 'body');

	$link->ParentTable = $table->Id();

	foreach($table->SelectPublishedRowArray() AS $row)
	{
		$link->Row = $row->Id();

		$treeView->Add('root',
				$row->Id(),
				$link->HTML($row),
				false,
				'help_page.gif');
	}
}
else
{
	$link = new \Pert\Link('Row.php', 'body');

	$link->Table = $table->Id();

	$treeNodesLink = new \Pert\Link('ObjectBrowser_rowsObjects.php');

	$treeNodesLink->ParentTable = $table->Id();

	foreach($table->SelectPublishedRowArray() AS $row)
	{
		$link->Row = $row->Id();

		$treeNodesLink->Row = $row->Id();

		if($row->VirtualRow())
			$treeView->Add('root',
					$row->Id(),
					$link->HTML($row),
					false,
					'contact.gif');
		else
			$treeView->Add('root',
					$row->Id(),
					$link->HTML($row),
					false,
					'help_page.gif',
					$treeNodesLink->Href());
	}
}

$treeView->isSubTree = true;

$treeView->width = '250px';

echo $treeView->Render();
